# Reference on this file: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
# You need to add BrowserStack access keys in order for the tests to work. Get a BrowserStack automate account, then go
# to the repository settings, the Secrets page and add secrets: BROWSERSTACK_USERNAME, BROWSERSTACK_ACCESS_KEY.
# In order to receive notifications in Slack, create a Slack bot,
# add the credentials to repository secrets SLACK_BOT_TOKEN (when installing the app) and SLACK_CHANNEL_ID (from within Slack).

name: Check upcoming browser versions

on: 
  schedule:
    - cron: '0 17 ? * MON-FRI'
env:
  BROWSERSTACK_USERNAME: ${{ secrets.BROWSERSTACK_USERNAME }}
  BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
  SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}

jobs:
  unit_tests:
    name: Run unit tests
    runs-on: ubuntu-latest
    timeout-minutes: 15
    concurrency: browserstack
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Cache the Node modules
        uses: actions/cache@v3
        with:
          path: node_modules
          key: nodemodules-${{ hashFiles('yarn.lock') }}
          restore-keys: nodemodules-
      - name: Install Node packages
        run: yarn install
      - name: Build
        run: yarn build
      - name: Run unit tests
        uses: nick-fields/retry@v2
        with:
          timeout_minutes: 5
          max_attempts: 2
          retry_on: error
          command: yarn test:browserstack:beta
          new_command_on_retry: yarn test:browserstack:beta --log-level debug
      - name: Post to a Slack channel
        id: slack
        uses: slackapi/slack-github-action@v1.23.0
        with:
          channel-id: ${{ vars.SLACK_CHANNEL_ID }}
          slack-message: "An issue was found when running unit tests against beta versions of the browsers.\nPlease check the build:\n${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
        if: ${{ failure() }}
